
//按周 统计  
function callbackByWeek(target, retdata, datetime){
	
	if(callbackByTime(target, retdata, datetime+"所在周 收支统计")){//先画 总统计 信息
		//按天显示统计信息
		data = retdata.data;
		data[0]= ["周一","周二","周三","周四","周五","周六","周日"]; 
		drawMapByMonth( $(target).find(".map>div[name=map3]"), data, datetime+"所在周 收支统计", "按周统计");
	} 
}

//按年 统计  
function callbackByYear(target, retdata, datetime){
	if(callbackByTime(target, retdata, datetime+"年 收支统计")){//先画 总统计 信息
		//按天显示统计信息
		data = retdata.data;
		for(var index in data[0]){
			data[0][index] = data[0][index] + "月"; 
		}
		drawMapByMonth( $(target).find(".map>div[name=map3]"), data, datetime+" 收支统计", "按年统计");
	} 
}

//按月 统计  
function callbackByMonth(target, retdata, datetime){
	
	if(callbackByTime(target, retdata, datetime+"月 收支统计")){//先画 总统计 信息
		//按天显示统计信息
		data = retdata.data;
		for(var index in data[0]){
			data[0][index] = data[0][index] + "日"; 
		}
		drawMapByMonth( $(target).find(".map>div[name=map3]"), data, datetime+" 收支统计", "按天统计");
	}
}
//按月 统计 map
function drawMapByMonth(obj, data, title, name){

	obj.highcharts({
			exporting:{
				enabled: false
			},
		 	chart: {
	            type: 'spline',
	            zoomType: 'x'
	        },
	        title: {
	            text: title
	        },
	        subtitle: {
	            text: name
	        },
	        xAxis: {
        	   title: {
	              text: '日期'
	           },
	           minTickInterval: 1,
	           categories: data[0],
	           labels: {
	                formatter: function() {
	                    return this.value ;
	                }
	            }
	        }, 
	        yAxis: {
	            title: {
	                text: '金额(元)'
	            },
	            labels: {
	                formatter: function() {
	                    return this.value +'元';
	                    
	                }
	            }
	        },
	        exporting: {
	        	filename: title
	        },
	        credits:{
	        	enabled: true,
	        	href: 'http://ipark.teleframe.cn',
	        	text: 'ipark.teleframe.cn',
	        },
	        tooltip: {
	            crosshairs: true,
	            shared: true,  
	            pointFormat: '{series.name}: <b>{point.y} 元  </b>'
	        },
	        plotOptions: {
	            spline: {
	                marker: {
	                    radius: 4,
	                    lineColor: '#666666',
	                    lineWidth: 1
	                }
	            }
	        },
	        series: [{
	            name: '收入',
	            data: data[1]
	        }, {
	            name: '支出',
	            data: data[2]
	        }]
    });
}


//按天统计
function callbackByDay(target, retdata, datetime){
	callbackByTime(target, retdata, datetime+" 收支统计");
}	

//统计图map1 & mp2
function callbackByTime(target, retdata, datetimeTitle){
	//
	var payInOut = retdata.payInOut;//收支统计
	if(payInOut.length==0){
		alert("暂无数据");
		return false;
	}else if(payInOut.length <= 2){
		var data = new Array();
		var getAmount=0;
		var outAmount=0;
		for(var par in payInOut){
			if(payInOut[par].type==1){
				console.log("收入余额：" + payInOut[par].amount);
				getAmount = payInOut[par].amount;
			}else if(payInOut[par].type=2){
				console.log("支出金额：" + payInOut[par].amount);
				outAmount = NonNegative(payInOut[par].amount);
			}
		}
		var d = new Array();
		d.push("余额");
		d.push((getAmount - outAmount));
		data.push(d);
		var d = new Array();
		d.push("支出金额");
		d.push( outAmount );
		data.push(d);
		drawMapByDay( $(target).find(".map>div[name=map1]"), data, datetimeTitle, " 总收入("+getAmount+"元)");
	}else{
		alert("收支统计信息异常");
		return false;
	}
	var payStat = retdata.payStat;//按类别收支统计
	if(payStat.length <= 4){
		var data = new Array();
		for(var par in payStat){
			data[par] = new Array(); 
			if(payStat[par].category==1){
				data[par].push("停车预定费用");
				data[par].push( NonNegative(payStat[par].amount));
			}else if(payStat[par].category==2){
				data[par].push("预定小费");
				data[par].push( NonNegative(payStat[par].amount));
			}else if(payStat[par].category==4){
				data[par].push("车场提款");
				data[par].push( NonNegative(payStat[par].amount));
			}else if(payStat[par].category==3){
				data[par].push("账户充值");
				data[par].push( NonNegative(payStat[par].amount));
			}
		}
		drawMapByDay( $(target).find(".map>div[name=map2]"), data, datetimeTitle, "所占比例");
	}else{
		alert("按类别收支统计信息异常");
	}
	return true;
}
//按天统计map
function drawMapByDay(obj, data, title, name){

	obj.highcharts({
		exporting:{
			enabled: false
		},
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: true
        },
        title: {
            text: title
        },
        subtitle: {
            text: name
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        exporting: {
        	filename: title
        },
        credits:{
        	enabled: true,
        	href: 'http://ipark.teleframe.cn',
        	text: 'ipark.teleframe.cn',
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.y} 元'
                },
                showInLegend: true
            }
        },
        series: [{
            type: 'pie',
            name: name,
            data: data
        }]
    });
}

function switchtab( tabtype ){
	$("div.container>div.row").hide();
	$("div.container>div.row[type='"+tabtype+"']").show() ;
	if(window.console){
		//console.log("switchtab @ "+tabtype);
	}
}

$("ul.nav-tabs li").click(function(){
	var tab = $(this);
	var nav = tab.parent();
	$(nav).children("li").removeClass("active"); 
	tab.addClass("active");
	var type = $(this).attr("type");
	switchtab( type );
	return true;
});

